import { resolve } from "path";
import { log } from "console";
import * as fs from "fs";

import { PicGo } from "picgo";
// import { delay } from "./utils/utils";

import "./types";
import picgoGiteePlugin from "./utils/picgoGiteePlugin";

const PLUGINS_NAME = "gitee";

async function main({
  imgPathList,
  configFilePath = "",
}: {
  imgPathList: string[];
  configFilePath?: string;
}): Promise<void> {
  if (!configFilePath) configFilePath = resolve("./config.json");

  const picgo = new PicGo(configFilePath);

  picgo.on("beforeUpload", ctx => {
    // console.log("beforeUpload-pic-ctx: ", ctx);
  });

  picgo.on("beforeTransform", ctx => {
    // console.log("beforeTransform-pic-ctx: ", ctx);
  });

  picgo.on("finished", ctx => {
    // console.log(ctx.output); // [{fileName, width, height, extname, imgUrl}] <- 注意有imgUrl了。
  });

  picgo.on("failed", error => {
    // console.log({ error }); // [{fileName, width, height, extname, imgUrl}] <- 注意有imgUrl了。
  });

  picgo.pluginLoader.registerPlugin(PLUGINS_NAME, picgoGiteePlugin);

  if (!picgo.pluginLoader.hasPlugin(PLUGINS_NAME)) {
    return log("error, loadplugin is error");
  }

  console.log({ imgPathList, configFilePath });

  // const res = await picgo.upload(imgPathList);

  // console.log("res: ", res);
}

// test file

const imgPathList = [];
for (let eachPath of ["./test1.jpg", "./test2.jpg"]) {
  let absolutePath = resolve(eachPath);
  if (absolutePath && fs.statSync(eachPath)) {
    imgPathList.push(absolutePath);
  }
}

main({ imgPathList });

export default main;
